package com.stylefeng.guns.modular.system.task;

import java.net.URI;
import java.net.URL;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.apache.http.client.methods.HttpGet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.stylefeng.guns.core.util.DateUtil;
import com.stylefeng.guns.core.util.HttpClientUtil;
import com.stylefeng.guns.modular.system.constants.ImportContants;
import com.stylefeng.guns.modular.system.dao.RealtimeMapper;
import com.stylefeng.guns.modular.system.model.Realtime;

@Service
public class RealTimeGpImportTask {
	
	private Logger log = LoggerFactory.getLogger(RealTimeGpImportTask.class);
	
	private static final HttpClientUtil clientUtil = new HttpClientUtil();
	
	@Autowired
	private RealtimeMapper realtimeMapper;
	
	public void execute(boolean isHoliday) {
		if(isHoliday){
			String url = ImportContants.getReadTimeDataPathStr(1);
			HttpGet httpGet = HttpClientUtil.getGetRequest();
			try {
				URL http = new URL(url);
				URI uri = new URI(http.getProtocol(), http.getHost(), http.getPath(), http.getQuery(), null);
				httpGet.setURI(uri);
				String json = clientUtil.executeAndGetWithFailedRetry(httpGet, 6,10001);
				JSONObject jsonObj = JSON.parseObject(StringUtils.replace(StringUtils.replace(json, "(",""), ")", ""));
				if(jsonObj.getIntValue("pc") > 0){
					parseReadTimeData(jsonObj.getString("data"));
					int pages = jsonObj.getIntValue("pc");
					for(int i= 2; i <= pages ; i++){
						url = ImportContants.getReadTimeDataPathStr(i);
						http = new URL(url);
						uri = new URI(http.getProtocol(), http.getHost(), http.getPath(), http.getQuery(), null);
						httpGet.setURI(uri);
						json = clientUtil.executeAndGetWithFailedRetry(httpGet, 6,10001);
						jsonObj = JSON.parseObject(StringUtils.replace(StringUtils.replace(json, "(",""), ")", ""));
						parseReadTimeData(jsonObj.getString("data"));
					}
				}
			} catch (Exception e) {
				log.error("get readTime data is error!" + e);
			}finally{
				httpGet.abort();
			}
		}
	}
	
	private void parseReadTimeData(String result){
		String[] realTimes = StringUtils.substringsBetween(result, "\"", "\"");
		for(String real : realTimes){
			if(StringUtils.isNotEmpty(real)){
				String[] detail = StringUtils.split(real,",");
				String time = detail[1];
				String dec = detail[2];
				int type = Integer.valueOf(detail[3]);
				String scode = detail[4];

				
				Realtime realTimeData = new Realtime();
				realTimeData.setgDec(dec);
				realTimeData.setgScode(StringUtils.substring(scode, 0,6));
				realTimeData.setgType(type);
				Date ctime = DateUtil.parseTime(DateUtil.format(new Date(), "yyyy-MM-dd").concat(" ").concat(time));
				realTimeData.setgTime(ctime);
				if(ctime.getTime() < DateUtil.getBeforeDate("-5").getTime()){
					continue;
				}
				
				realTimeData = realtimeMapper.selectByDateTime(realTimeData);
				if(realTimeData == null){
					realTimeData = new Realtime();
					realTimeData.setgDec(dec);
					realTimeData.setgScode(StringUtils.substring(scode, 0,6));
					realTimeData.setgType(type);
					realTimeData.setgTime(DateUtil.parseTime(DateUtil.format(new Date(), "yyyy-MM-dd").concat(" ").concat(time)));
					realtimeMapper.insert(realTimeData);
				}
			}
		}
	}
}
